package com.techstar.ats.codec;

import io.netty.buffer.ByteBuf;
import com.techstar.ats.dxp.AtsMessage;

public interface MessageEncoder<T extends AtsMessage> extends Encoder<T> {
	default void encodeHead(ByteBuf buf, T value) {
		buf.writeByte(0xff);
		buf.writeShort(11 + this.bytes(value));
		buf.writeByte(0);
		buf.writeShort(8 + this.bytes(value));
		buf.writeInt((int) (System.currentTimeMillis() / 1000L));
		buf.writeShort(1);
		buf.writeShort(value.id);
	}

	default void encode(ByteBuf buf, T value) {
		this.encodeHead(buf, value);
		this.encodeBody(buf, value);
	}

	void encodeBody(ByteBuf buf, T value);

	int bytes(T value);
}
